package com.pub.check;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;

public class DebugCheck {
    private static boolean blnDebug = false; //測試用，正式要改為false
    public DebugCheck() {
    }

    /**
     * 印出存在於request物件中所有的cookie name and value
     * @param httpServletRequest HttpServletRequest
     * @return boolean
     */
    public boolean printCookie(HttpServletRequest httpServletRequest) {
        boolean blnFlag = true;
        //String userId = "", strUsername = "";
        try {
            //步驟1：從cookie中得到userId
            Cookie[] cookies = httpServletRequest.getCookies();
            if (blnDebug) {
                System.out.println("PublicCheck.java (printCookie) --> ");
            }
            if (blnDebug) {
                System.out.println("cookies -> " + cookies);
            }
            if (cookies == null) {
                return blnFlag = false; //無cookies時
            }
            String ckName = null;
            String ckValue = null;
            if (blnDebug) {
                System.out.println("cookies.length=" + cookies.length);
            }
            for (int i = 0; i < cookies.length; i++) {
                ckName = cookies[i].getName();
                ckValue = cookies[i].getValue();
                if (blnDebug) {
                    System.out.println(ckName + " : " + ckValue);
                }
                /*if (ckName != null && "u_id".equalsIgnoreCase(ckName)) {
                    userId = cookies[i].getValue(); //取到portal上的empno的cookie
                }*/
            } //end-for
        } catch (Exception ex) {
            System.out.println("\nPublicCheck.java" +
                               " method:printCookie --> Exception=" +
                               ex.toString());
        }
        return blnFlag;
    }
}
